{
 "metadata": {},
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Optimization and fit demo\n",
      "=========================\n",
      "\n",
      "This is a quick example of creating data from several [Bessel\n",
      "functions](http://docs.scipy.org/doc/scipy/reference/special.html) and\n",
      "finding local maxima, then fitting a curve using some spline functions\n",
      "from the\n",
      "[scipy.interpolate](http://docs.scipy.org/doc/scipy/reference/interpolate.html)\n",
      "module. The [enthought.chaco](http://code.enthought.com/chaco/) package\n",
      "and [wxpython](http://www.wxpython.org/) are used for creating the plot.\n",
      "[PyCrust](http://wiki.wxpython.org/index.cgi/PyCrust) (which comes with\n",
      "wxpython) was used as the python shell."
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from enthought.chaco.wx import plt\n",
      "from scipy import arange, optimize, special\n",
      "\n",
      "plt.figure()\n",
      "plt.hold()\n",
      "w = []\n",
      "z = []\n",
      "x = arange(0,10,.01)\n",
      "\n",
      "for k in arange(1,5,.5):\n",
      "   y = special.jv(k,x)\n",
      "   plt.plot(x,y)\n",
      "   f = lambda x: -special.jv(k,x)\n",
      "   x_max = optimize.fminbound(f,0,6)\n",
      "   w.append(x_max)\n",
      "   z.append(special.jv(k,x_max))\n",
      "   \n",
      "plt.plot(w,z, 'ro')\n",
      "from scipy import interpolate\n",
      "t = interpolate.splrep(w, z, k=3)\n",
      "s_fit3 = interpolate.splev(x,t)\n",
      "plt.plot(x,s_fit3, 'g-')\n",
      "t5 = interpolate.splrep(w, z, k=5)\n",
      "s_fit5 = interpolate.splev(x,t5)\n",
      "plt.plot(x,s_fit5, 'y-')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#class left\n",
      "\n",
      "# ![](files/attachments/OptimizationAndFitDemo1/chacoscreenshot.png)\n",
      "Optimization Example"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}